package com.console.operations.planmgr;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.planmgr.FinancePlanAuditPage;

public class OhgFinancePlanAudit extends OperationBase {
	
	public OhgFinancePlanAudit(UITestBase tb){
		super(tb);
	}
	
	public boolean isfinancePlanExistInAudit = false;
	
	public boolean isstatuscorrected = false;
	
	public void auditfianceplan() throws Exception{
		switchToDefaultContent();
		FinancePlanAuditPage afpp = new FinancePlanAuditPage(tb);
		waitForElementDisplayed(afpp.panelhead);
		String financeplantitle = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.INVESTBATCH_OHG_TEMPLATE, "名称");
		//动态设置DefaultElement的findValue
		afpp.auditLink.setfindValue("//tr[td[text()='"+ financeplantitle +"']]/td[a[text()='审核']]/a");
		waitForElementExist(afpp.pagination);
		elementToBeClickable(afpp.pagination);
		List<WebElement> subelement = afpp.pagination.getNativeElement().findElements(By.tagName("li"));
		int pagecount = Integer.parseInt(subelement.get(subelement.size()-1).getText());
		log.info("page count is " + pagecount);
		//分页处理，直到找到相应控件为止
		if(pagecount>1){
			for(int i=1;i<pagecount;i++){
				if(isPageContainsStr(financeplantitle)){
					waitForElementDisplayed(afpp.auditLink);
					afpp.auditLink.click();
					isfinancePlanExistInAudit = true;
					break;
				}else{
					afpp.presentpageLink.setfindValue("//li[a[text()='"+ (i+1) +"']]/a");
					afpp.presentpageLink.click();
					if(i!=pagecount-1){
						afpp.presentpageLink.setfindValue("//li[a[text()='"+ (i+2) +"']]/a");
						waitForElementExist(afpp.presentpageLink);
					}
				}
			}
		}else{
			if(isPageContainsStr(financeplantitle)){
				isfinancePlanExistInAudit = true;
				waitForElementDisplayed(afpp.auditLink);
				afpp.auditLink.click();
			}
		}
		if(!isfinancePlanExistInAudit){
			log.error("审核里找不到该理财计划 \"" + financeplantitle + "\"");
			return;
		}
		waitForElementDisplayed(afpp.platformSelect);
		String platform = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.INVESTBATCH_OHG_TEMPLATE, "发布平台");
		afpp.platformSelect.selectOptionByText(platform);
		//点击通过按钮
		afpp.passbutton.click();
		waitForElementDisplayed(afpp.passsubmitbutton);
		//点击弹出框上的通过按钮
		afpp.passsubmitbutton.click();
		waitForPageContainsStr("已通过");
		String dcstatus = DBUtil.queryOneCell("select dc_status from fiz_plan where vc_name = ?", new Object[]{financeplantitle});
		isstatuscorrected = dcstatus.equals("02");
		if(!isstatuscorrected){
			log.error("审核后状态没有变为02（后台发布中）");
			return;
		}
	}
}
